package com.xtaller.test;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-08-09
 * @Description:
 */
public class R extends HashMap<String, Object> {
    public R() {
    }

    public static ResponseEntity error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR, "server error");
    }

    public static ResponseEntity error(String message) {
        return error(HttpStatus.BAD_REQUEST, message);
    }

    public static ResponseEntity error(HttpStatus status, String message) {
        return error(status, message, new JSONObject(), 0);
    }

    public static ResponseEntity error(HttpStatus status, String message, int isRefresh) {
        return error(status, message, new JSONObject(), isRefresh);
    }

    public static ResponseEntity error(HttpStatus status, String message, Object object, int isRefresh) {
        R r = new R();
        r.put("message", message);
        r.put("result", object);
        r.put("isRefresh", isRefresh);
        r.put("timestamp", "123");
        r.put("code", status.value());
        return new ResponseEntity(r, status);
    }

    public static ResponseEntity ok(String message) {
        return ok(message, new JSONObject());
    }

    public static ResponseEntity ok(Object object) {
        return ok("操作成功", object);
    }

    public static ResponseEntity ok(String message, Object object) {
        return ok(message, object, 0);
    }

    public static ResponseEntity ok(String message, Object object, int isRefresh) {
        R r = new R();
        r.put("message", message);
        r.put("result", object);
        r.put("timestamp", 123);
        r.put("isRefresh", isRefresh);
        r.put("code", 200);
        return new ResponseEntity(r, HttpStatus.OK);
    }
    @Override
    public R put(String key, Object val) {
        super.put(key, val);
        return this;
    }
}
